//
//  Number11Cell.m
//  7.13苏宁框架
//
//  Created by mini on 15/7/17.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "Number11Cell.h"
#import "Common.h"
#import "CollectionViewCell.h"
@implementation Number11Cell


-(instancetype)init
{
    self = [super init];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"Number11Cell" owner:nil options:nil]lastObject];
        [self addDatd];
    }
    return self;
}

-(void)awakeFromNib
{
    //给单元格添加手势
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goodsClick:)];
    [self addGestureRecognizer:tap];
}
- (void)addDatd {
    
    //创建自定义布局对象
    CustomLayout *layout = [[CustomLayout alloc]init];
    layout.delegate = self;
    //添加CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    self.collectionView = collectionView;
    self.collectionView.height = 1910;
    self.collectionView.backgroundColor = [UIColor colorWithRed:248.0/255 green:248.0/255 blue:248.0/255 alpha:1];
    [self addSubview:collectionView];
    //注册单元格
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"detailCell"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (CGFloat)waterflowLayout:(CustomLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
    _model = self.total[indexPath.item];
    CGFloat w = [_model.w floatValue];
    CGFloat h = [_model.h floatValue];
    return w/h*h;
}

-(NSArray*)total
{
    _total = [NSArray array];
    _total = [GoodSDetailModel modelArrayWithFilename:@"GoodSDetail.plist"];
    return _total;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.total.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detailCell" forIndexPath:indexPath];
    [cell setModel:_total[indexPath.item]];
    return cell;
}

-(void)goodsClick:(UITapGestureRecognizer*)tap
{
    //获取点击的位置
    CGPoint point = [tap locationInView:self];
    //根据点击的位置判断它的子视图是哪个
    for (CollectionViewCell *subView in self.collectionView.subviews) {
        if (CGRectContainsPoint( subView.frame, point) ) {
            GoodSDetailModel *model = subView.model;
            XQModel *xqmodel = [[XQModel alloc]init];
            xqmodel.image = model.image;
            xqmodel.name = model.name;
            xqmodel.price = model.price;
            xqmodel.oldprice = model.oldprice;
            xqmodel.know = model.know;
            xqmodel.fuwu = model.fuwu;
            xqmodel.hpl = model.hpl;
            xqmodel.person = model.person;
            xqmodel.pj = model.pj;
            xqmodel.detailpj = model.detailpj;
            xqmodel.smallimage = model.smallimage;
            xqmodel.type = model.type;
            //把模型对象传给控制器，让控制器跳转到商品详情页面
            if ([self.delegate respondsToSelector:@selector(pushQX:)]) {
                [self.delegate pushQX:xqmodel];
            }
        }
    }
}
@end
